package trab4;

import java.util.*;

import sim.core.*;
import sim.util.*;

public class ServerDB {
	
	static RandomList<Server> servers = new RandomList<Server>();
	static TreeMap<Integer, Server> k2s = new TreeMap<Integer, Server>();

	static int store( Server n) {
		servers.add( n) ;
		k2s.put( n.key, n ) ;
		return n.key ;
	}

	Set<Integer> keys() {
		return k2s.keySet();
	}

	static void dispose( Server n) {
		if (n != null) {
			if( k2s.remove( n.key) != null ) {
				servers.remove( n ) ;
				NodeDB.dispose(n) ;
				n.dispose();			
			}
		}
	}

	public static int size() {
		return k2s.size();
	}

	static RandomList<EndPoint> endpoints() {
		RandomList<EndPoint> res = new RandomList<EndPoint>();
		for (Node i : servers)
			res.add(i.endpoint);
		return res;
	}

	static Server randomServer() {
		return servers.randomElement();
	}
}